/*
 *  debug.c
 *
 *  MyQQ debugger.
 *
 *  Copyright (C) 2008  Huang Guan
 *
 *  2008-01-31 Created.
 *
 */


#include "debug.h"
#include "util.h"
#include "qqdef.h"

static int dbg_term = 0, dbg_file = 0, log_day = 0, dbg_print = 0;
static int enablehex_recv=0,enablehex_send=0;
static FILE* fp_log = NULL;
static char dir[128]={0,}, filename[160];
static void init_file_path();

static char* to_gb( char* s )
{
	static char buff[1024]={0};
//	memset( print_buf, 0, PRINT_BUF_SIZE );
	utf8_to_gb( s, buff,MIN( strlen(s),1024 ) );
	return buff;
}


void print_error(char* file, char* function, int line, const char *fmt, ...)
{

	va_list args;
	char printbuf[512];
	int i;
	if( !dbg_term && !dbg_file )
		return;
	va_start(args, fmt);
	i=vsprintf( printbuf, fmt, args );
	printbuf[i] = 0;
	va_end(args);
#ifdef __WIN32__
	utf8_to_gb( printbuf, printbuf, i );
#endif
	if( dbg_term ){
		printf("%s(%d): %s\n", function, line, printbuf);
	}
	if( dbg_file ){
		time_t t = time( NULL );
		struct tm* tm1 = localtime(&t);
		if( !tm1 ) return;
		if( tm1->tm_mday != log_day ){
			init_file_path();
		}
		char tmp[16];
		strftime( tmp, 15, "%X", tm1 );
		fprintf( fp_log, "%s [%s]%s(%d): %s\n", tmp, file, function, line, printbuf);
		fflush( fp_log );
	}
}

char* hex_str(unsigned char *buf, int len, char* outstr )
{


	const char *set = "0123456789abcdef";
	char *tmp;
	unsigned char *end;
	if (len > 1024)
	len = 1024;
	end = buf + len;
	tmp = &outstr[0];
	while (buf < end)
	{
		  *tmp++ = set[ (*buf) >> 4 ];
		  *tmp++ = set[ (*buf) & 0xF ];
		  *tmp++ = ' ';
		  buf ++;
	}
	*tmp = '\0';
	return outstr;
}

char* hex_str2(unsigned char *buf, int len )
{
	static char retValue[1024*3];
	const char *set = "0123456789abcdef";
	char *tmp;
	unsigned char *end;
	if (len > 1024)
	len = 1024;
	end = buf + len;
	tmp = &retValue[0];
	while (buf < end)
	{
		  *tmp++ = set[ (*buf) >> 4 ];
		  *tmp++ = set[ (*buf) & 0xF ];
		  *tmp++ = ' ';
		  buf ++;
	}
	*tmp = '\0';
	return retValue;
}

char* hex_char( char *buf, char* outstr )
{
	const char *set = "0123456789abcdef";
	*outstr++ = set[ (*buf) >> 4 ];
	*outstr++ = set[ (*buf) & 0xF ];
	*outstr++ = ' ';
	return outstr;
}

char* hex_char2( char *buf )
{
	const char *set = "0123456789abcdef";
	static char *tmp[2];
	*tmp[0] = set[ (*buf) >> 4 ];
	*tmp[1] = set[ (*buf) & 0xF ];
	*tmp[2] = '\0';
	return tmp[0];
}

void hex_dump( unsigned char * buf, int len )
{

	char str[KB(4)];
	if( dbg_term )
		puts( hex_str( buf, len, str ) );
	if( dbg_file ){
		fputs( hex_str( buf, len, str ), fp_log );
		fprintf( fp_log, "\n" );
		fflush( fp_log );
	}
	//fprintf( stderr, hex_str( buf, len ) );
}

void debug_setting_switch_by_str(char* arg){
	if(( arg[0]== '+' ) && ( arg[1]=='f' )){
		printf("debug file : on\n");
		debug_file_on();

	}else if(( arg[0]== '-' ) && ( arg[1]=='f' )){
		printf("debug file : off\n");
		debug_file_off();

	}else if(( arg[0]== '+' ) && ( arg[1]=='t' )){
		printf("debug screen : on\n");
		debug_term_on();

	}else if(( arg[0]== '-' ) && ( arg[1]=='t' )){
		printf("debug screen : off\n");
		debug_term_on();

	}else if(( arg[0]== '+' ) && ( arg[1]=='r' )){
		printf("debug recv packet hex_dump : on\n");
		debug_hexpack_recv(1);

	}else if(( arg[0]== '-' ) && ( arg[1]=='r' )){
		printf("debug recv packet hex_dump : off\n");
		debug_hexpack_recv(0);

	}else if(( arg[0]== '+' ) && ( arg[1]=='s' )){
		printf("debug send packet hex_dump : on\n");
		debug_hexpack_send(1);

	}else if(( arg[0]== '-' ) && ( arg[1]=='s' )){
		printf("debug send packet hex_dump : off\n");
		debug_hexpack_send(0);

	}else{
		printf("err");
	}
}

void debug_term_on()
{
	dbg_term = 1;
}

void debug_term_off()
{
	dbg_term = 0;
}

void debug_print_on()
{
	dbg_print = 1;
}

void debug_print_off()
{
	dbg_print = 0;
}

void init_file_path()
{
	char tmp[64];
	time_t t = time( NULL );
	struct tm* tm1 = localtime(&t);
	if( !tm1 ){
		perror("log.c init_file_path: ERROR GETTING SYSTEM TIME.");
	}
	log_day = tm1->tm_mday;
	strftime( tmp, 64, "/%Y-%m-%d.txt", tm1 );
	if( access( dir, 0 )!=0 ){
		mkdir_recursive( dir );
	}
	strcpy( filename, dir );
	strcat( filename, tmp );
	if( fp_log )
		fclose( fp_log );
	fp_log = fopen( filename, "aw" );
//	fp_log = fopen( filename, "w" );
}

void debug_file_on()
{
	if( dbg_file )
		return;
	init_file_path();
	dbg_file = 1;
}

void debug_file_off()
{
	if( !dbg_file )
		return;
	dbg_file = 0;
	if( fp_log )
		fclose( fp_log );
}

void debug_set_dir(char* str)
{

	strcpy( dir, str );
}

void debug_hexpack_recv(int swh){
	enablehex_recv=swh;
}

void debug_hexpack_send(int swh){
	enablehex_send=swh;
}

void debug_hex_pak_send(unsigned char* buf,char* title)
{
	if(buf[0]==02 && buf[1]==18) return;
	if(!enablehex_send) return;
	printf("\nsend==========\\/=================%s\n",title);
	hex_dump(buf,1024);
	printf("\n==============/\\=================%s\n",title);
	return;
}

void debug_hex_pak_recv(unsigned char* buf,char* title)
{
	if(buf[0]==02 && buf[1]==18) return;
	if(!enablehex_recv) return;
	printf("\nrecv==========\\/=================%s\n",title);
	hex_dump(buf,1024);
	printf("\n==============/\\=================%s\n",title);
	return;
}
